
package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider.BillDetailsActivity;
import com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider.MercgantListActivity;
import com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider.MerchantpromotionActivity;
import com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider.MyPointsActivity;
import com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider.MyStatisticsActivity;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import ezy.ui.view.NoticeView;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = "HomeActivity";
    private int hmCode;
    private ViewPager Pager;
    private List<View> list;
    private TextView n_ToRZ, e_contract, i_details, i_tax_instead, m_settings, bill_details, show_today_pc;
    private LinearLayout m_wallet, extension, home_my_statistics, Integral_income, transaction_details, my_real_machine, point_exchange, partner;
    private int HomeCode, ecHomeCode;
    private ImageView novice_introduction, tuiguanggongl;

    private TextView home_showbalance, home_level, integral, home_name;
    public static String[] notices = new String[]{
            "伪装者:胡歌演绎'痞子特工'",
            "无心法师:生死离别!月牙遭虐杀",
            "花千骨:尊上沦为花千骨",
            "综艺饭:胖轩偷看夏天洗澡掀波澜",
            "碟中谍4:阿汤哥高塔命悬一线,超越不可能",
    };

    NoticeView vNotice;

    private static final int TIME_EXIT = 3000;
    private static int isExit = 0;
    //实现按两次后退才退出
    private CountDownTimer mCountDownTimer = new CountDownTimer(TIME_EXIT, 1000) {
        @Override
        public void onTick(long l) {
            isExit++;

        }

        @Override
        public void onFinish() {
            isExit = 0;
        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return false;
    }

    private void exit() {
        if (isExit < 1) {
            Toast.makeText(getApplicationContext(), "再点击一次返回退出程序", Toast.LENGTH_SHORT).show();
            mCountDownTimer.start();
        } else {
            isExit = 0;
            Toast.makeText(getApplicationContext(), "退出程序", Toast.LENGTH_SHORT).show();
            mCountDownTimer.cancel();
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            System.exit(0);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        n_ToRZ = findViewById(R.id.nameToRZ);
        n_ToRZ.setOnClickListener(this);
        e_contract = findViewById(R.id.electronic_contract);
        e_contract.setOnClickListener(this);
        m_wallet = findViewById(R.id.my_wallet);
        m_wallet.setOnClickListener(this);
        i_details = findViewById(R.id.income_details);
        i_details.setOnClickListener(this);
        i_tax_instead = findViewById(R.id.individual_tax_instead);
        i_tax_instead.setOnClickListener(this);
        m_settings = findViewById(R.id.my_extension);
        m_settings.setOnClickListener(this);
        extension = findViewById(R.id.extension);
        extension.setOnClickListener(this);
        home_my_statistics = findViewById(R.id.home_my_statistics);
        home_my_statistics.setOnClickListener(this);
        Integral_income = findViewById(R.id.Integral_income);
        Integral_income.setOnClickListener(this);
        bill_details = findViewById(R.id.bill_details);
        bill_details.setOnClickListener(this);
        transaction_details = findViewById(R.id.transaction_details);
        transaction_details.setOnClickListener(this);
        my_real_machine = findViewById(R.id.my_real_machine);
        my_real_machine.setOnClickListener(this);

        home_showbalance = findViewById(R.id.home_showbalance);
        home_level = findViewById(R.id.home_level);
        integral = findViewById(R.id.integral);
        home_name = findViewById(R.id.home_name);
        point_exchange = findViewById(R.id.point_exchange);
        point_exchange.setOnClickListener(this);
        partner = findViewById(R.id.partner);
        partner.setOnClickListener(this);
        show_today_pc = findViewById(R.id.show_today_pc);
        novice_introduction = findViewById(R.id.novice_introduction);
        novice_introduction.setOnClickListener(this);
        // Integral_income.setCompoundDrawablePadding(40);
        tuiguanggongl = findViewById(R.id.tuiguanggongl);
        tuiguanggongl.setOnClickListener(this);


        vNotice = (NoticeView) findViewById(R.id.notice);
        vNotice.start(Arrays.asList(notices));
        vNotice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Toast.makeText(HomeActivity.this, notices[vNotice.getIndex()], Toast.LENGTH_SHORT).show();
            }
        });

        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String name = sharedPreferences.getString("name", "");
        home_name.setText(name);
        String money = sharedPreferences.getString("money", "");
        if(money.equals("0")) {
            Double zx = Double.parseDouble(money);
            DecimalFormat decimalFormatq = new DecimalFormat("0.00");
            String xz = decimalFormatq.format(zx);
            home_showbalance.setText(xz);
        }else {
            home_showbalance.setText(money);
        }


        String level = sharedPreferences.getString("level", "");
        home_level.setText("会员等级："+level);
        String integra = sharedPreferences.getString("integra", "");
        if(integra.equals("0")) {
            Double z = Double.parseDouble(integra);
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            String x = decimalFormat.format(z);
            integral.setText(x);
        }else{
            integral.setText(integra);
        }

        intView();

        Timer timer = new Timer();
        timer.schedule(new task(), 300000);
    }

    class task extends TimerTask {

        @Override
        public void run() {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message msg = handler1.obtainMessage();
                    msg.what = 20;
                    handler1.sendMessage(msg);
                }
            }).start();

            intView();
        }
    }

    Handler handler1 = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 20:
                    Timer timer = new Timer();
                    timer.schedule(new task(), 300000);

            }
        }
    };


    @Override
    public void onResume() {
        intView();
        super.onResume();
    }


    private void intView() {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/home_info");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    HomeCode = jsonObject.optInt("code", 0);
                    String data = jsonObject.optString("data", null);
                    if (!TextUtils.isEmpty(data)) {
                        JSONObject jsonObjec = new JSONObject(data);
                        String name = jsonObjec.optString("name", "");
                        if (name.equals("null")) {
                            home_name.setText("Spiriter");
                        } else {
                            home_name.setText(name);
                        }

                        String integra = jsonObjec.optString("integral", "");
                        if(integra.equals("0")) {
                            Double z = Double.parseDouble(integra);
                            DecimalFormat decimalFormat = new DecimalFormat("0.00");
                            String x = decimalFormat.format(z);
                            integral.setText(x);
                        }else{
                            Double zzz = Double.parseDouble(integra);
                            DecimalFormat decimalFormat = new DecimalFormat("0.00");
                            String xzz = decimalFormat.format(zzz);
                            integral.setText(xzz);
                        }


                        String money = jsonObjec.optString("money", "");
                        if(money.equals("0")) {
                            Double zx = Double.parseDouble(money);
                            DecimalFormat decimalFormat4 = new DecimalFormat("0.00");
                            String xz = decimalFormat4.format(zx);
                            home_showbalance.setText(xz + "");
                        }else {
                            Double zxx = Double.parseDouble(money);
                            DecimalFormat decimalFormat4 = new DecimalFormat("0.00");
                            String xzx = decimalFormat4.format(zxx);
                            home_showbalance.setText(xzx + "");
                        }


                        String level = jsonObjec.optString("level", "");
                        if (level.equals("null")) {
                            home_level.setText("会员等级："+"暂无");
                        } else {
                            home_level.setText("会员等级："+level);
                        }
                        float a = Float.parseFloat(integra);
                        float b = a / 200;
                        int c = (int) b;
                        double d = c * (0.2);
                        DecimalFormat decimalFormat3 = new DecimalFormat("0.00");
                        String p = decimalFormat3.format(d);
                        show_today_pc.setText(p + "");
                        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE); //私有数据
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        if (name.equals("null")) {
                            editor.putString("name", "Spiriter");
                        } else {
                            editor.putString("name", name);
                        }

                        editor.putString("money", money);
                        editor.putString("integra", integra);
                        if (level.equals("null")) {
                            editor.putString("level", "暂无");
                        } else {
                            editor.putString("level", level);
                        }
                        editor.commit();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (HomeCode == 200) {
                }
                if (HomeCode == 500) {
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

//    private void intViewPsger() {
//        list =new ArrayList<View>();
//        ImageView iv=new ImageView(this);
//        iv.setImageResource(R.drawable.guide_01);
//        list.add(iv);
//        ImageView iv1=new ImageView(this);
//        iv1.setImageResource(R.drawable.guide_02);
//        list.add(iv1);
//        ImageView iv2=new ImageView(this);
//        iv2.setImageResource(R.drawable.guide_03);
//        list.add(iv2);
//        Pager.setAdapter(new MyPagerAdapter());
//        Pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
//
//
//    }
//
//    class MyPagerAdapter extends PagerAdapter{
//        @Override
//        public int getCount() {
//            return  list.size();
//        }
//        @Override
//        public boolean isViewFromObject( View vrg0, Object arg1) {
//            return vrg0==arg1;
//        }
//
//       @Override
//        public Object instantiateItem( ViewGroup container, int position) {
//           container.addView(list.get(position));
//           return  list.get(position);
//        }
//
//        @Override
//        public void destroyItem(  ViewGroup container, int position,  Object object) {
//            container.removeView(list.get(position));
//        }
//    }


    @Override
    public void onClick(View v) {

        int id = v.getId();
        switch (id) {
            case R.id.nameToRZ:
                Intent Nameintent = new Intent(HomeActivity.this, NameActivity.class);
                startActivity(Nameintent);
                break;
            case R.id.electronic_contract:
                Intent Ecintent = new Intent(HomeActivity.this, ElectroniContractActivity.class);
                startActivity(Ecintent);
                break;
            case R.id.my_wallet:
                Intent Mwintent = new Intent(HomeActivity.this, MyWalletActivity.class);
                startActivity(Mwintent);
                break;
            case R.id.income_details:
                Intent Incomeintent = new Intent(HomeActivity.this, IncomeDetailsActivity.class);
                startActivity(Incomeintent);
                break;
            case R.id.individual_tax_instead:
                Intent TIintent = new Intent(HomeActivity.this, Tax_Instead_Activity.class);
                startActivity(TIintent);
                break;
            case R.id.my_extension:
                Intent Extintent = new Intent(HomeActivity.this, ExtensionActivity.class);
                startActivity(Extintent);
                break;
            case R.id.extension:
                Intent Mertintent = new Intent(HomeActivity.this, MerchantpromotionActivity.class);
                startActivity(Mertintent);
                break;
            case R.id.home_my_statistics:
                Intent Msttintent = new Intent(HomeActivity.this, MyStatisticsActivity.class);
                startActivity(Msttintent);
                break;
            case R.id.Integral_income:
                Intent MyPintent = new Intent(HomeActivity.this, MyPointsActivity.class);
                startActivity(MyPintent);
                break;
            case R.id.bill_details:
                Intent BDintent = new Intent(HomeActivity.this, BillDetailsActivity.class);
                startActivity(BDintent);
                break;
            case R.id.transaction_details:
                Intent HTintent = new Intent(HomeActivity.this, TransactiondetailsActivity.class);
                startActivity(HTintent);
                break;
            case R.id.my_real_machine:
                Intent HMintent = new Intent(HomeActivity.this, MyrealMachineActivity.class);
                startActivity(HMintent);
                break;
            case R.id.point_exchange:
                Intent HPintent = new Intent(HomeActivity.this, PointexChangeActivity.class);
                startActivity(HPintent);
                break;
            case R.id.partner:

                SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
                String role = sharedPreferences.getString("role", "");
                if (role.equals("4")) {
                    Intent HMerintent = new Intent(HomeActivity.this, MercgantListActivity.class);
                    startActivity(HMerintent);
                } else {
                    Intent recordIntent = new Intent(HomeActivity.this, DailiMercgantListActivity.class);
                    startActivity(recordIntent);
                }


                break;
            case R.id.novice_introduction:
                Intent HNerintent = new Intent(HomeActivity.this, NewNoviceActivity.class);
                startActivity(HNerintent);
                break;
            case R.id.tuiguanggongl:
                Intent HTerintent = new Intent(HomeActivity.this, TggonglActivity.class);
                startActivity(HTerintent);
                break;

        }
    }
}
